import os
import yaml

current_dir = os.path.dirname(os.path.abspath(__file__))
DEFAULT_CONFIG_YAML_PATH = os.path.join(current_dir, "..", "config.yaml")


class Configuration:
    """
    yaml配置类(单例)
    """

    # 类变量，用于存储单例实例
    _instance = None

    def __new__(cls, config_path: str = DEFAULT_CONFIG_YAML_PATH):
        if cls._instance is None:
            cls._instance = super(Configuration, cls).__new__(cls)
            cls._instance.config_path = config_path if config_path is not None else DEFAULT_CONFIG_YAML_PATH
            # 初始化配置
            cls._instance._initialize_config()
        return cls._instance

    def _initialize_config(self):
        print(f'初始化yaml配置[{self.config_path}]')
        with open(self.config_path, "r", encoding='utf-8') as file:
            self.config_data = yaml.safe_load(file)

    def set_system_settings(self, setting_key: str, setting_val: str):
        """
        修改yaml中的系统配置
        :param setting_key: 配置key
        :param setting_val: 值
        :return:
        """
        self.config_data["system_settings"][setting_key] = setting_val

    def set_ruge_settings(self, setting_key: str, setting_val: str):
        """
        修改yaml中的Ruge配置
        :param setting_key: 配置key
        :param setting_val: 值
        :return:
        """
        self.config_data["ruge_settings"][setting_key] = setting_val

    def get_system_settings(self, key: str):
        """
        获取系统配置
        :param key:
        :return:
        """
        return self.config_data["system_settings"][key]

    def get_ruge_settings(self, key: str):
        """
        获取Ruge配置
        :param key:
        :return:
        """
        return self.config_data["ruge_settings"][key]

    def get_nacos_settings(self, key: str):
        """
        获取Nacos配置
        :param key:
        :return:
        """
        return self.get_ruge_settings("nacos_config")[key]

    def get_test_settings(self, key: str):
        """
        获取测试配置
        :param key:
        :return:
        """
        return self.config_data["test_settings"][key]


def init_config(config_path: str = DEFAULT_CONFIG_YAML_PATH):
    """
    初始化配置
    :param config_path:
    :return:
    """
    Configuration(config_path)


def get_system_settings(key: str):
    """
    获取系统配置
    :param key:
    :return:
    """
    config = Configuration()
    return config.get_system_settings(key)


def get_ruge_settings(key: str):
    """
    获取Ruge配置
    :param key:
    :return:
    """
    config = Configuration()
    return config.get_ruge_settings(key)


def get_nacos_settings(key: str):
    """
    获取Nacos配置
    :param key:
    :return:
    """
    config = Configuration()
    return config.get_nacos_settings(key)


def get_test_settings(key: str):
    """
    获取测试配置
    :param key:
    :return:
    """
    config = Configuration()
    return config.get_test_settings(key)


def set_system_settings(setting_key: str, setting_val: str):
    """
    设置系统配置
    :param setting_val:
    :param setting_key:
    :return:
    """
    config = Configuration()
    config.set_system_settings(setting_key, setting_val)


def set_ruge_settings(setting_key: str, setting_val: str):
    """
    设置Ruge配置
    :param setting_val:
    :param setting_key:
    :return:
    """
    config = Configuration()
    config.set_ruge_settings(setting_key, setting_val)
